//
//  HWlingjuanzhongxingViewController.m
//   
//
//  Created by 陈道斌 on 2021/3/8.
//  Copyright © 2021 Chendaobin. All rights reserved.
//

#import "HWlingjuanzhongxingViewController.h"
#import "HWlingqunzhongxinTableViewCell.h"
#import "HWyouhuiqunModel.h" // 数据模型

#import "HWMyVouchersViewController.h" // 我的优惠券


@interface HWlingjuanzhongxingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic)  NSString *p;
@property(strong,nonatomic)  NSString *max_p;

@property(strong,nonatomic) NSMutableArray *dataArr;

@property(strong,nonatomic) NSArray *biaotiArr;
@property(strong,nonatomic) NSArray *icoArr;
@property(strong,nonatomic) UILabel *jifenLabel; //积分Label
@property(strong,nonatomic) NSArray *shujuArr;
@property(strong,nonatomic) UIButton *shuaxingBut;

@property(strong,nonatomic)UILabel *jifenshituLabel; // 最新的积分金额


@end

@implementation HWlingjuanzhongxingViewController

-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {
        
        CGFloat h =    CFLScaleValue*263-CF_StatusBarHeight;
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, h  +CF_StatusBarHeight, kScreenW,kScreenH-(h+CF_StatusBarHeight)) style:UITableViewStylePlain];
         _mainaTableVie.delegate = self;
         _mainaTableVie.dataSource = self;
        _mainaTableVie.backgroundColor = [UIColor redColor];
        _mainaTableVie.backgroundColor = RGB(247, 247, 247);
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
//        [self.mainaTableVie mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(0);
//            make.top.mas_equalTo(h + CF_StatusBarHeight);
//            make.width.mas_equalTo(kScreenW);
//            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
//        }];

    }
    return   _mainaTableVie ;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   //   [self shezhichangyongdeshuxiing];
    
    [self setupUI];
    
    // [self fasongwangluoqing];
    
    [self addrefreshControl];
    
    
}



#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {
    
   
    
    self.p = @"1";
     
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];

    self.dataArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"coupons",
                                       @"member_id":[HWLocallyStoredData getCode],
                                     
                                       };
       
       [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
           
           // [SVProgressHUD dismiss];
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
               
                   self.dataArr = [HWyouhuiqunModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               
               
               
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
               
               [self.mainaTableVie reloadData];
               
    
           }   else {
               
               
               
               [MBProgressHUD showError:response[@"message"]];
           }
        
           
           
       } failure:^(NSString *error) {
           
           
             [MBProgressHUD showError:error];
    
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
       }];
    
    
}

// 上拉加载

- (void)onRefresh {
    
    
   
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        return ;
         
        
      }
                   

      NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     
      NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
     
      NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
     
      NSString *singMD5 = [WMUtil md5HexDigest:singStr];

        NSDictionary *parametes = @{
                       @"p" :self.p,
                       @"ios" : @"1",
                       @"ct" :  @"app",
                       @"ac" : @"coupons",
                       @"member_id":[HWLocallyStoredData getCode],
                   
        };
         
        [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
                  NSMutableArray *mutableArr = [HWyouhuiqunModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];


                 for (int i = 0; i < mutableArr.count; i++) {
                     [self.dataArr addObject:mutableArr[i]];

                 }

                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                 [self.mainaTableVie reloadData];
                 
      
             }
          
             
             
         } failure:^(NSString *error) {
             
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
            
         }];
      
    
}




//- (void)fasongwangluoqing {
//    
//      
//    
//       //重制密码
//    
//        int  r =  [WMUtil getRandomNumber:101 to:998];
//      
//      
//         NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
//         NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],r];
//         
//      
//         
//         NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
//         
//         NSString *singMD5 = [WMUtil md5HexDigest:singStr];
//
//    
//         NSDictionary *parametes = @{
//                                           
//                                            @"ios" : @"1",
//                                            @"ct" :  @"app",
//                                            @"ac" : @"renwu",
//                                            @"uid" :uidStr,
//                                            @"t" : tStr,
//                                            @"sign" : singMD5,
//                                         
//                                            };
//            
//           [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//                
//              
//                
//                
//                NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//                
//                if ([codeStr intValue] == 1) {
//                    
//                  
//                    
//                
//                
//                    [self.mainaTableVie reloadData];
//                    
//                    
//         
//                } else  {
//                    
//                     [MBProgressHUD showError:response[@"message"]];
//                    
//                }
//             
//                
//                
//            } failure:^(NSString *error) {
//                [MBProgressHUD showError:@"兑换失败"];
//                
//                
//            }];
//         
//      
//    
//    
//}






//- (void)shezhichangyongdeshuxiing {
//
//    self.icoArr = @[@"welfare_signin",@"welfare_new",@"welfare_message",@"welfare_game",@"welfare_share"];
//    self.dataArr = @[@"每日签到",@"新手任务",@"每日任务",@"游戏评论",@"邀请好友"];
//
//    self.biaotiArr = @[@"签到得积分，连续签到积分翻倍哦！",@"完成新手任务，送海量积分哦！",@"每天福利等您享，积分送不停！",@"优质的评论信息可以获得积分奖励哦！",@"独乐乐不如众乐乐，快去邀请好友吧！"];
//
//    // 重置密码
//
//
//}


- (void)setupUI {
   
    self.view.backgroundColor = [UIColor whiteColor];
//    self.view.backgroundColor = RGB(242, 242, 242);
    [self setUPbeijingView];
    
    [self navigaView];
    

    [self.view addSubview:self.mainaTableVie];
    [self.mainaTableVie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(CFLScaleValue*263-CF_StatusBarHeight + CF_StatusBarHeight);
        make.width.mas_equalTo(kScreenW);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
    
}


- (void)setUPbeijingView {
    
    
    CGFloat h =    CFLScaleValue*263-CF_StatusBarHeight;
    
    
 //     kScreenW/263.0  = kScreenH/376.0
// kScreenW/263.0* 376.0
    
    UIView *beijingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, h +CF_StatusBarHeight)];
   //  beijingView.backgroundColor = RGB(242, 242, 242);
  //    beijingView.backgroundColor =  [UIColor bm_colorGradientChangeWithSize:CGSizeMake(kScreenW, 138+CF_StatusBarHeight) direction:IHGradientChangeDirectionVertical startColor:RGB(73, 74, 86) endColor:RGB(32, 32, 42)];
    
    [self.view addSubview:beijingView];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, h+CF_StatusBarHeight)];
      imageView.image = [UIImage imageNamed:@"youhuiqubanner"];
    imageView.backgroundColor = [UIColor redColor];
    imageView.hidden = NO;
    [beijingView addSubview:imageView];
    
    // 263  376
    
  //263/376 = kScreenW / 2 ；
    
  //   kScreenW*(376/263)
    
    
//
//    UIImageView *jingxiang = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-149-10, 165+CF_StatusBarHeight-124-5, 149, 124)];
//    // jingxiang.image = [UIImage imageNamed:@"welfare_con"];
//   // jingxiang.backgroundColor = [UIColor yellowColor];
//    jingxiang.hidden = YES;
//    [imageView addSubview:jingxiang];
    
    
//    UIImageView *renwujifenwenziimageview = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-147*CFLScaleValue-149-10)/2, 165+CF_StatusBarHeight-50*CFLScaleValue-40, 147*CFLScaleValue, 50*CFLScaleValue)];
//    renwujifenwenziimageview.image = [UIImage imageNamed:@"welfare_message_tit"];
//   // renwujifenwenziimageview.backgroundColor = [UIColor redColor];
//    [imageView addSubview:renwujifenwenziimageview];
//
    
    
    
//    UIButton *jifenbutt = [UIButton  createButtonWithTitle:@"我的积分" Image:@"zongjifenico" Target:nil Selector:nil];
//    jifenbutt.frame = CGRectMake(20, CF_StatusBarHeight+44+20, 80, 16);
//    jifenbutt.backgroundColor = [UIColor clearColor];
//    [jifenbutt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:10];
//    [self.view   addSubview:jifenbutt];
//
//    self.jifenshituLabel  = [[UILabel alloc]initWithFrame:CGRectMake(42, jifenbutt.bottom+6, 300, 32)];
//    self.jifenshituLabel.text = @"310";
//    self.jifenshituLabel.font = [UIFont systemFontOfSize:20];
//    self.jifenshituLabel.textColor = RGB(255, 255, 255);
//    [self.view addSubview: self.jifenshituLabel];
    
//
//    UIButton *fanhuiBut = [UIButton buttonWithType:UIButtonTypeCustom];
//     fanhuiBut.frame = CGRectMake(kScreenW-20-66, CF_StatusBarHeight+44+45 , 66, 26);
//     fanhuiBut.backgroundColor = [UIColor  clearColor];
//    //  [fanhuiBut setImage:[UIImage imageNamed:@"fanhuijiemianIco"] forState:UIControlStateNormal];
//     [fanhuiBut addTarget:self action:@selector(jifenshang) forControlEvents:UIControlEventTouchUpInside];
//    [fanhuiBut setTitle:@"积分商城" forState:UIControlStateNormal];
//    [fanhuiBut  setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
//    fanhuiBut.layer.cornerRadius = 13;
//    fanhuiBut.layer.masksToBounds = YES;
//    fanhuiBut.titleLabel.font = [UIFont  systemFontOfSize:13];
//    fanhuiBut.backgroundColor =  [UIColor bm_colorGradientChangeWithSize:CGSizeMake(66, 26) direction:IHGradientChangeDirectionLevel startColor:RGB(255, 182, 10) endColor:RGB(246, 106, 5)];
//    [self.view addSubview:fanhuiBut];
//
//
    
//    UIButton *choujiangBut = [UIButton buttonWithType:UIButtonTypeCustom];
//     choujiangBut.frame = CGRectMake(kScreenW-20-66-12-66, CF_StatusBarHeight+44+45 , 66, 26);
//     choujiangBut.backgroundColor = [UIColor  clearColor];
//    //  [fanhuiBut setImage:[UIImage imageNamed:@"fanhuijiemianIco"] forState:UIControlStateNormal];
//     [choujiangBut addTarget:self action:@selector(choujiangBut) forControlEvents:UIControlEventTouchUpInside];
//    [choujiangBut setTitle:@"积分抽奖" forState:UIControlStateNormal];
//    [choujiangBut  setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
//    choujiangBut.layer.cornerRadius = 13;
//    choujiangBut.layer.masksToBounds = YES;
//    choujiangBut.titleLabel.font = [UIFont  systemFontOfSize:13];
//    choujiangBut.backgroundColor =  [UIColor bm_colorGradientChangeWithSize:CGSizeMake(66, 26) direction:IHGradientChangeDirectionLevel startColor:RGB(255, 182, 10) endColor:RGB(246, 106, 5)];
//    [self.view addSubview:choujiangBut];
//
    
    
    
    

    
    
    
   
    
    
    
    
//
//    UIView *jifenView = [[UIView alloc]initWithFrame:CGRectMake(15, 165+CF_StatusBarHeight+20-40, kScreenW-15*2, 40)];
//   jifenView.backgroundColor = [UIColor whiteColor];
//    jifenView.layer.cornerRadius = 6;
//    jifenView.layer.masksToBounds = YES;
//    jifenView.hidden = YES;
//    [beijingView addSubview:jifenView];
//
//    UIButton *jifengbuton =  [UIButton buttonWithType:UIButtonTypeCustom];
//    [jifengbuton setTitle:@"积分商城" forState:UIControlStateNormal];
//    jifengbuton.frame = CGRectMake( kScreenW-15*2-73-15, 8, 73, 25);
//    jifengbuton.layer.cornerRadius = 12;
//    jifengbuton.layer.masksToBounds = YES;
//    jifengbuton.backgroundColor = RGB(233, 241, 255);
//    jifengbuton.titleLabel.font = [UIFont systemFontOfSize:12];
//    [jifengbuton setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
//    [jifengbuton addTarget:self action:@selector(jifengbutonTap) forControlEvents:UIControlEventTouchUpInside];
//
//    [jifenView addSubview:jifengbuton];
    
  
    
    
//    CGSize size =  [WMUtil strName:@"我的积分：500" fontSize:[UIFont systemFontOfSize:14] boundingRectWithSize:CGSizeMake(1000, 200)];
//    self.jifenLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, size.width, 40)];
//    self.jifenLabel.text = @"我的积分：500";
//    self.jifenLabel.font = [UIFont systemFontOfSize:14];
//    self.jifenLabel.textColor = RGB(51, 51, 51);
//    [jifenView addSubview:   self.jifenLabel];
//
//     self.jifenLabel.attributedText = [WMUtil  attributedStringWithText:@"我的积分：500" withRangText:@"500" withColor:RGB(253, 178, 50) withFont:[UIFont systemFontOfSize:14]];
//
//
//    UIButton *shulianbuton =  [UIButton buttonWithType:UIButtonTypeCustom];
//    shulianbuton.frame = CGRectMake(self.jifenLabel.right+5, 0, 30, 40);
//    [shulianbuton setImage:[UIImage imageNamed:@"welfare_refresh"] forState:UIControlStateNormal];
//    self.shuaxingBut = shulianbuton;
//    [shulianbuton addTarget:self action:@selector(shuxingBut) forControlEvents:UIControlEventTouchUpInside];
//
//    [jifenView addSubview:shulianbuton];
    
    
    
}




- (void)navigaView  {
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10,StatusBarHeight+17, 40, 17);
   // button.backgroundColor = [UIColor redColor];
    [button setImage:[UIImage imageNamed:@"details_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-70)/2, StatusBarHeight+17, 70, 17)];
    textlabel.text = @"积分任务";
    textlabel.text = [HWjianzhiduiObject keylocalization:@"积分任务"];
    textlabel.textAlignment =  NSTextAlignmentCenter;
    textlabel.textColor = [UIColor whiteColor];
    textlabel.font = [UIFont systemFontOfSize:16];
    textlabel.hidden  = YES ;
    [self.view addSubview:textlabel];
    
    
    
    UILabel *jifenmingxitextlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-85)-20, StatusBarHeight+17, 85, 17)];
    jifenmingxitextlabel.text = @"我的优惠券";
    jifenmingxitextlabel.text =  [HWjianzhiduiObject keylocalization:@"我的优惠券"];
    jifenmingxitextlabel.textAlignment =  NSTextAlignmentCenter;
       jifenmingxitextlabel.textColor = [UIColor whiteColor];
       jifenmingxitextlabel.font = [UIFont systemFontOfSize:14];
    jifenmingxitextlabel.userInteractionEnabled = YES;
       [self.view addSubview:jifenmingxitextlabel];
    
    UITapGestureRecognizer *tap13 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(youhuanquanjiemian)];

    [jifenmingxitextlabel  addGestureRecognizer:tap13];
    
    
    
    
    
    
    
       
       
    
    
    
    
    
  //  jiludushao
    
       UIButton *lishijilubutton = [UIButton buttonWithType:UIButtonTypeCustom];
       lishijilubutton.frame = CGRectMake(kScreenW-35,StatusBarHeight+17, 17, 17);
      // button.backgroundColor = [UIColor redColor];
       lishijilubutton.hidden = YES;
       [lishijilubutton setImage:[UIImage imageNamed:@"jiludushao"] forState:UIControlStateNormal];
       [lishijilubutton addTarget:self action:@selector(jiludushaoTap) forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:lishijilubutton];
    

}



- (void)youhuanquanjiemian {
    
    HWMyVouchersViewController *myvouchers =  [[HWMyVouchersViewController alloc]init];
    [self.navigationController pushViewController:myvouchers animated:YES];
    
    
}



- (void)buttonTap {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)jiludushaoTap {
    DLog(@"记录");

    
  
    
}




- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
   // [self.navigationController setNavigationBarHidden:YES animated:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO];
    
    
}


#pragma  mark -------- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return   self.dataArr.count;
    
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    
  
 
      static NSString *cellID = @"cellID";
        
      HWlingqunzhongxinTableViewCell   *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

        
        if (!cell) {
            cell =    [[NSBundle mainBundle]loadNibNamed:@"HWlingqunzhongxinTableViewCell" owner:nil options:nil].firstObject;
            
            
        }
    
    
      [cell.xianqingBut  addTarget:self action:@selector(youxiangxiqnaing:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
      HWyouhuiqunModel *model =  self.dataArr[indexPath.row];
    
      cell.model  = model;
      return  cell;
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return 186;
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
  
    

    
}



- (void)jifenshang {
    
   
}




- (void)youxiangxiqnaing:(UIButton *)sender  {
    
    
    
    NSLog(@"%@",[[[[sender superview] superview] superview] superview]);
    
    HWlingqunzhongxinTableViewCell *cell =  (HWlingqunzhongxinTableViewCell *)[[[[sender superview] superview] superview] superview];
    
    
    NSIndexPath *index =  [self.mainaTableVie indexPathForCell:cell];

    HWyouhuiqunModel *model =  self.dataArr[index.row];

    HWGameDetailController *game = [[HWGameDetailController alloc]init];
    game.gameId = model.gid;
   [[[HWViewUtils  getCurrentVC] navigationController]  pushViewController:game animated:YES];

    
    
}
 
// HWGameDetailsViewController *game = [[HWGameDetailsViewController alloc]init];
//game.gameId = model.gid;
//[[[HWViewUtils  getCurrentVC] navigationController]  pushViewController:game animated:YES];





@end
